﻿function ClsDynaTreeHelper() {
    var self = this;

    // variable
    self.TreeData = null;
    self.ItemId = -1;
    self.ArrId = new Array();
    self.ArrDelId = new Array();
    self.DivId = "tree";
    self.DialogId = "dialog-dynatree";

    // config
    self.LoadManageUrl = "";
    self.ManageUrl = "";
    self.DialogWidth = 600;
    self.DialogPosition = ["middle", "center"];
    self.PrefixKey = "dynaTreeNodeKey_";
}

// function
ClsDynaTreeHelper.prototype.Init = function () {
    var self = this;

    $("#" + self.DivId).dynatree({
        checkbox: true,
        selectMode: 3,
        children: self.TreeData,

        // The following options are only required, if we have more than one tree on one page:
        //        initId: "treeData",
        cookieId: self.DivId,
        idPrefix: self.DivId + "-"
    });
}

ClsDynaTreeHelper.prototype.LoadManageData = function (itemId)
{
    var self = this;

    self.ItemId = itemId;

    // deselect all
    $("#" + self.DivId).dynatree("getTree").reload();

    var data = { itemId: self.ItemId };

    $.ajax({
        url: self.LoadManageUrl,
        data: JSON.stringify(data),
        type: 'POST',
        contentType: 'application/json',
        beforeSend: function () {
            CommonHelper.AjaxLoading();
        },
        success: function (result) {
            CommonHelper.StopAjaxLoading();

            if (result.Message == null || result.Message == "") {
                self.ArrId = self.GetArrId(result.Result);

                self.CheckNode();

                $("#" + self.DialogId).dialog({
                    resizable: false,
                    width: self.DialogWidth,
                    height: 400,
                    position: self.DialogPosition,
                    buttons: {
                        "Ok": function () {
                            self.ManageData(false);
                        },
                        "Cancel": function () {
                            $(this).dialog("close");
                        },
                        "Apply": function () {
                            self.ManageData(true);
                        }
                    }
                });
            }
            else {
                CommonHelper.ShowMsg("Error: " + result.Message, MsgIconType.Error);
            }
        },
        error: function (result) {
            CommonHelper.StopAjaxLoading();
            CommonHelper.ShowMsg("Error: " + CommonPhrase.CommonError, MsgIconType.Error);
        }
    });
}

// private
ClsDynaTreeHelper.prototype.ManageData = function (isApply) {
    var self = this;

    var data;
    var msg = null;

    if (isApply) {
        msg = CommonPhrase.Success;
    }

    data = { itemId : self.ItemId, arrDelId: self.ArrDelId, arrNewId: self.GetCheckedArrId() };

    $.ajax({
        url: self.ManageUrl,
        data: JSON.stringify(data),
        type: 'POST',
        contentType: 'application/json',
        beforeSend: function () {
            CommonHelper.AjaxLoading();
        },
        success: function (result) {
            CommonHelper.StopAjaxLoading();
            self.ProcessCallBackSuccess(result, msg, isApply);
        },
        error: function (result) {
            CommonHelper.StopAjaxLoading();
            CommonHelper.ShowMsg("Error: " + CommonPhrase.CommonError, MsgIconType.Error);
        }
    });
}

// private
ClsDynaTreeHelper.prototype.ProcessCallBackSuccess = function (result, msg, isApply) {
    var self = this;

    if (result.Message == null || result.Message == "") {
        if (msg != null) {
            alert(msg);
        }
        self.ArrDelId = self.GetCheckedArrId();

        if (!isApply) {
            $("#" + self.DialogId).dialog("close");
        }

        AdminHelper.LoadList();
    }
    else {
        CommonHelper.ShowMsg("Error: " + result.Message, MsgIconType.Error);
    }
}

ClsDynaTreeHelper.prototype.GetArrId = function (result) {
    // define in runtime
}

// private
ClsDynaTreeHelper.prototype.CheckNode = function () {
    var self = this;

    $("#" + self.DivId).dynatree("getRoot").visit(function (node) {
        for (var i = 0; i < self.ArrId.length; i++)
        {
            var id = self.ArrId[i];

            if (node.data.key == self.PrefixKey + id) {
                node.select(true);

                break;
            }
        }
    });

    self.ArrDelId = self.GetCheckedArrId();
}

// private
ClsDynaTreeHelper.prototype.GetCheckedArrId = function () {
    var self = this;
    var arrSelectedNode = $("#" + self.DivId).dynatree("getTree").getSelectedNodes();
    var result = new Array();

    for (var i = 0; i < arrSelectedNode.length; i++) {
        var node = arrSelectedNode[i];

        if (node.data.key.indexOf(self.PrefixKey) !== -1) {
            result.push(self.ConvertToId(node.data.key));
        }
    }

    return result;
}

// private
ClsDynaTreeHelper.prototype.ConvertToId = function (key) {
    var self = this;

    return parseInt(key.replaceAll(self.PrefixKey, ""));
}

var DynaTreeHelper = new ClsDynaTreeHelper();